package dfl.bysj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dfl.bysj.entity.ReserveTime;
import dfl.bysj.entity.Seat;
import dfl.bysj.entity.SeatState;
import dfl.bysj.entity.VO.SeatStateVO;
import dfl.bysj.mapper.SeatStateMapper;
import dfl.bysj.service.ReserveStateService;
import dfl.bysj.service.ReserveTimeService;
import dfl.bysj.service.SeatStateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-05-17
 */
@Slf4j
@Service
public class SeatStateServiceImpl extends ServiceImpl<SeatStateMapper, SeatState> implements SeatStateService {

    SeatStateMapper seatStateMapper;

    ReserveTimeService reserveTimeService;

    ReserveStateService reserveStateService;

    @Autowired
    public void setReserveStateService(ReserveStateService reserveStateService) {
        this.reserveStateService = reserveStateService;
    }

    @Autowired
    public void setReserveTimeService(ReserveTimeService reserveTimeService) {
        this.reserveTimeService = reserveTimeService;
    }

    @Autowired
    public void setSeatStateMapper(SeatStateMapper seatStateMapper) {
        this.seatStateMapper = seatStateMapper;
    }

    @Override
    public List<SeatStateVO> getAllSeatStateVOBySeaId(Integer seaId) {
        List<SeatStateVO> seatStateVOS = new ArrayList<>();
        List<SeatState> list = seatStateMapper.selectList(
                new QueryWrapper<SeatState>()
                        .eq("sea_id", seaId)
        );
        list.stream()
                .forEach(s -> {
                    SeatStateVO seatStateVO = new SeatStateVO();
                    seatStateVO.setReserveTime(reserveTimeService.getById(s.getResTimeId()));
                    seatStateVO.setReserveState(reserveStateService.getById(s.getResTimeId()));
                    seatStateVOS.add(seatStateVO);
                });
        return seatStateVOS;
    }

    /**
     * 获取座位状态：已预约/可预约
     *
     * @param seaId
     * @return
     */
    @Override
    public String getStateName(Integer seaId) {
        //看是否能拿到3的预约状态
        List<SeatState> seatStates = seatStateMapper.selectList(
                new QueryWrapper<SeatState>()
                        .eq("sea_id", seaId)
                        .eq("res_sta_id", 3)
        );
        if (seatStates.isEmpty()) {
            //说明该作为都是可预约/已预约
            //找到已预约
            List<SeatState> seatStates1 = seatStateMapper.selectList(
                    new QueryWrapper<SeatState>()
                            .eq("sea_id", seaId)
                            .eq("res_sta_id", 2)
            );
            if (seatStates1.isEmpty()) {
                return "可预约";
            } else {
                return "已预约";
            }
        }
        return "不可预约";
    }

    @Override
    public void insertSeatStatesByResSta(List<Seat> seatList, Integer reserveState) {

        List<ReserveTime> reserveTimes = reserveTimeService.getAllReserveTime();
        seatList
                .forEach(s -> {
                    reserveTimes
                            .forEach(s1 -> save(new SeatState(s.getSeaId(), s1.getResTimeId(), reserveState)));
                });

    }

    @Override
    public void updateReserveStateBySeaId(Integer seaId) {

        SeatState seatState = new SeatState();
        String state = getStateName(seaId);
        if (state.equals("不可预约")) {
            seatState.setResStaId(1);
        } else {
            seatState.setResStaId(3);
        }
        update(seatState, new QueryWrapper<SeatState>()
                .eq("sea_id", seaId));
        log.info(String.valueOf(seaId));
    }


}
